package com.hnucm.cooklife.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.SPUtils;
import com.hnucm.cooklife.R;
import com.hnucm.cooklife.base.Utils;
import com.hnucm.cooklife.base.utils.Constant;
import com.hnucm.cooklife.ui.activity.PostSomeActivity;
import com.hnucm.cooklife.ui.activity.SearchWordActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SearchHistoryAdapter extends RecyclerView.Adapter<SearchHistoryAdapter.SHistoryHolder> {

    private Context mContext;
    private List<String> mSearchHistoryList =  new ArrayList<>();
    private boolean isNightMode;

    public SearchHistoryAdapter(Context context, List<String> searchHistoryList) {
        mContext = context;
        mSearchHistoryList.addAll(searchHistoryList);
        isNightMode = SPUtils.getInstance(Constant.CONFIG_SETTINGS)
                .getBoolean(Constant.KEY_NIGHT_MODE,false);
    }

    public void setSearchHistoryList(List<String> searchHistoryList){
        mSearchHistoryList.clear();
        mSearchHistoryList.addAll(searchHistoryList);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public SHistoryHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.search_history_item, parent, false);
        return new SHistoryHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SHistoryHolder holder, int position) {
        holder.mHistoryText.setText(mSearchHistoryList.get(position));
        holder.mHistoryDelete.setOnClickListener(v -> {
            Utils.deleteSearchHistory(mSearchHistoryList.get(position),mContext);
            //todo Event

        });

        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, PostSomeActivity.class);
            intent.putExtra(Constant.KEY_KEYWORD,mSearchHistoryList.get(position));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return mSearchHistoryList != null ? mSearchHistoryList.size() : 0;
    }

    class SHistoryHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.item_history_text)
        TextView mHistoryText;
        @BindView(R.id.item_history_delete)
        ImageView mHistoryDelete;


        public SHistoryHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
